package com.example.schoolmanage.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.schoolmanage.domain.PageResult;
import com.example.schoolmanage.domain.Student;
import com.example.schoolmanage.domain.StudentAdd;
import com.example.schoolmanage.mapper.StudentMapper;
import com.example.schoolmanage.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author ayuan
* @description 针对表【STUDENT】的数据库操作Service实现
* @createDate 2023-01-31 14:42:18
*/

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Override
    public Student selectOneByStuId(Integer id) {

        return studentMapper.selectOneByStuId(id);

    }
    @Override
    public Student getStuCousbyStuId(Integer id){

        return studentMapper.getStuCousbyStuId(id);

    }

    @Override
    public Student getStubyStuId(Integer sId) {

        return  studentMapper.getStubyStuId(sId);

    }

    @Override
    public boolean addStudent(Student Stu) {
        //new一个新的student，只接收需要插入到sql的value中的值，并且获取系统时间到StuCreateddate属性中
        Student student = new Student();
        student.setStuId(0);
        student.setStuName(Stu.getStuName());
        student.setStuSex(Stu.getStuSex());
        Date day = new Date();//获取系统当前时间
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        student.setStuCreateddate(df.format(day));//将Date时间转换成特定格式的字符串String
        student.setStuCreateddate(day);
        return  studentMapper.addStudent(student);

    }

    @Override
    public boolean addStudent2(StudentAdd student2) {
        //new一个新的student，只接收需要插入到sql的value中的值，并且获取系统时间到StuCreateddate属性中
        Student student = new Student();
        student.setStuId(0);
        student.setStuName(student2.getStuName());
        student.setStuSex(student2.getStuSex());

        Date day = new Date();//获取系统当前时间
        student.setStuCreateddate(day);
        return  studentMapper.addStudent(student);
    }


    //mybatisPlus自带分页方法，可做参考
    public List<Student> selectPage(Integer pagenum, Integer pagesize) {
        Page page = new Page(pagenum, pagesize);
        IPage<Student> students = studentMapper.selectPage(page,null);
        return students.getRecords();
    }

    @Override
    public List<Student> getallbypage(Integer pagenum, Integer pagesize) {

        Page page = new Page(pagenum, pagesize);

        //2版本用List<>接收
        //List<Student> students = studentMapper.getallbypage(page);

        //3版本用Page<>接收
        Page<Student> students = studentMapper.getallbypage(page);

       //2版本直接返回List<>格式的students集合
        //return students;

        //3版本返回Page<>格式的student下的.getRecord()集合，该集合是List<>格式，存储了查询结果
        return students.getRecords();
    }

    @Override
    public PageResult<Student> getallVpage(Integer pagenum, Integer pagesize) {
        Page page = new Page(pagenum, pagesize);

        Page<Student> students = studentMapper.getallbypage(page);

        long totle = students.getTotal();
        List<Student> students1 = students.getRecords();

        return new PageResult(totle,students1);
    }

    @Override

    public List<Student> getStuCouspage(Integer pagenum,Integer pagesize){

        Page<Student> page = new Page<>(pagenum,pagesize);
        Page<Student> students =  studentMapper.getStuCous(page);
                return students.getRecords();
    }



}




